<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="../include/tag.jsp"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<title>${schedule == null ? '创建' : '编辑'}赛事日程</title>
<%@include file="../include/commonFile.jsp" %>
<link rel="stylesheet" href="${ctx}/css/common/list.css">
<link rel="stylesheet" href="${ctx}/css/common/form.css">
<link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
<style type="text/css">
</style>
</head><body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">
	<%@include file="../include/sidebar.jsp"%>
	<!--内容-->
	<section>
		<div class="section-main">

			<!-- 正文请写在这里 -->
			<div class="add-form-content">
			<form id="myForm" class="layui-form mt20" method="post" action="${ctx}/competition/schedule/save.do">
				<input type="hidden" name="id" value="${schedule.id}"/>
				<input type="hidden" name="projectId" value="${project.id}"/>
				<div class="layui-form-item">
					<label class="layui-form-label">比赛日<span class="f-verify-red">*</span></label>
					<div class="layui-input-inline">
						<input class="layui-input" name="playDayStr" lay-verify="playDayStr" type="text" readonly
							value='<fmt:formatDate value="${schedule.playDay}" pattern="yyyy-MM-dd" />'
						/>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">赛事地点<span class="f-verify-red">*</span></label>
					<div class="layui-input-inline">
						<input type="text" name="place" lay-verify="place" placeholder="请输入赛事地点" class="layui-input" value="${schedule.place}">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">日程距离<span class="f-verify-red">*</span></label>
					<div class="layui-input-inline" style="width: 150px!important;">
			      		<input type="text" name="distance" lay-verify="distance" placeholder="请设置当日赛程目标" class="layui-input" value="${schedule.distance}">
			      	</div>
					<div class="layui-form-mid layui-word-aux">KM</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
				   		<a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit lay-filter="*">立即提交</a>
						<a href="${ctx}/competition/schedule/list.do?projectId=${project.id}" class="layui-btn layui-btn-primary">取消</a>
				  	</div>
				</div>
			</form>
			</div>
		</div><!--底部-->
		<%@include file="../include/footer.jsp" %>
	</section>
</div>


<script>
	function ajaxSubmit(playDayStr, projectId, id) {
	    var isRepeat = false;
	    var desc = "";
	    $.ajax({
	        type: 'POST',
	        async: false, // 使用同步的方法
	        data: {
	        	playDayStr: playDayStr,
	        	projectId: projectId,
	        	id: id
	        },
	        dataType: 'json',
	        success: function (data) {
	            isRepeat = data.success;
	            desc = data.description;
	        },
	        url: '${ctx}/competition/schedule/checkPlayDay.do'
	    });
	   	var result = {
   			isRepeat : isRepeat,
   			desc : desc		
	   	}
	    return result;
	}
    $(function () {
        layui.use(['form', 'laydate'], function () {
            var form = layui.form, laydate = layui.laydate;
            //日期
            var start = {
                elem: 'input[name=playDayStr]',
                type: 'date',
                min : '${startTime}',
                format : 'yyyy-MM-dd'
            };
            laydate.render(start);
            form.verify({
                playDayStr : function(value){
                    if(value == ""){
                        return "比赛日不能为空";
                    }
                    var result = ajaxSubmit(value, '${project.id}', '${schedule.id}');
                    if(!result.isRepeat){
                        return result.desc;
                    }
                },
                place : function(value){
                    if(value == ""){
                        return "赛事地点不能为空";
                    }
                },
                distance : function(value){
                    if(value == ""){
                        return "日程距离不能为空";
                    }
                    var pattern = /^[0-9]+(.[0-9]{1})?$/;
                    if (!pattern.test(value)) {
                        return "日程距离格式不正确";
                    }
                }
            });

            //监听提交
            form.on('submit', function(data) {
    			//loading层
    			var loadIndex = layer.load(1, {
    			  shade: [0.1,'#fff'] //0.1透明度的白色背景
    			});
    			var action = $("#myForm").attr("action");
    			$.post(action, $('#myForm').serialize(), function(res) {
    				layer.close(loadIndex);
                	setTimeout(function() {
    					if (res.success) {
    						util.layerMsgSuccess("提交成功", function () {
    							location.href = "${ctx}/competition/schedule/list.do?projectId=${project.id}";
    						});
    					} else {
                            util.layerMsgError("提交失败")
    					}
                	}, 500);// 延时0.5秒
    			});
    			return false;
    		});
        });
    })

</script>
<script type="text/javascript">
    txz.initHeader({
        nav:[{
            name: '赛事项目管理',
            curr: true
        },{
            name: '${project.title}',
            curr: true
        },{
            name: '编辑赛事日程',
            href: '${ctx}/competition/schedule/list.do?projectId=${project.id}'
        },{
            name: '${schedule == null ? '创建' : '编辑'}赛事日程',
            curr: true
        }],
        btns:[{
            name: '返回',
            href: 'javascript:history.back();',
            icon: 'back'
        }]
    });
</script>
</body>
</html>